﻿using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class AttachmentService : BaseService<Attachment>, IAttachmentService
    {
        public IHouseAttachmentService houseAttachmentService { get; set; }
        public AttachmentDTO[] GetAttachments()
        {
            return GetAll().ToList().Select(a=>ToDTO(a)).ToArray();
        }

        public AttachmentDTO[] GetByHouseId(long houseId)
        {
            return houseAttachmentService.GetAll()
                .Include(ha=>ha.Attachment)
                .AsNoTracking()
                .Where(ha=>ha.HouseId== houseId)
                .ToList()
                .Select(ha=>ToDTO(ha.Attachment)).ToArray();
        }

        private AttachmentDTO ToDTO(Attachment entity)
        {
            AttachmentDTO dto = new AttachmentDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.Name = entity.Name;
            dto.IconName = entity.IconName;
            return dto;
        }
    }
}
